package com.yy.tools.arry
{
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	public class ArrayTools
	{
		
		/**
		 *给一个数组求和 
		 * @param input
		 * @param nums
		 * @return 
		 * 
		 */
		public static function sum(input:Array):Number{
			var sum:Number = 0;
			var len:int = input.length;
			for(var i:uint = 0;i<len;i++){
				sum+=(input[i] is Number ? input[i] : 0);
			}
			return sum;
		}
		
		/**
		 *数组中是否含有 某元素 
		 * @param input
		 * @param elem
		 * @return 
		 * 
		 */		
		public static function contains(input:Array, elem:Object):Boolean{
			var i:int = 0;
			var len:int = input.length;
			for(i=0; i<len; i++) {
				if(input[i] == elem) {
					return true;
				}
			}
			return false;
			
		}
		
		/**
		 *删除某个元素 
		 * @param input
		 * @param elem
		 * @return 
		 * 
		 */		
		public static function del(input:Array, elem:Object):Object{
			var len:uint = input.length;
			for(var i:uint = 0;i<len;i++){
				if(input[i]==elem){
					input.splice(i,1);
					return elem;
				}
			}
			return null;
		}
		
		/**
		 *返回一个没有空元素新的数组 
		 * @param input
		 * @return 
		 * 
		 */
		public static function getNonNullArray(input:Array):Array{
			var i:int,l:int;
			var newArr:Array = new Array();
			for(i=0,l=input.length;i<l;i++){
				if(!input[i]){
					newArr.push(input[i]);
				}
			}
			return newArr;			
		}
		
		/**
		 *	取得翻页数据  
		 * @param param    
		 * @param pageNum 第几页
		 * @param maxNum 页大小
		 * @return 
		 * 
		 */
		public static function runPage(param:Array,pageNum:int,maxNum:int):Array
		{
			var arr:Array = new Array();
			var countMax:int = maxNum>(param.length - maxNum * (pageNum-1))?param.length - maxNum * (pageNum-1):maxNum; 
			for(var i:int = (pageNum-1)*maxNum; i < countMax+((pageNum-1)*maxNum);i++)
			{
				arr.push(param[i]);
			}
			return arr;
		}
		
		/**
		 *比较两个数组是否相等 
		 * @param arr1
		 * @param arr2
		 * @return 
		 * 
		 */
		public static function compareArr(arr1:Array, arr2:Array):Boolean {
			var i:int;
			var len:int = arr1.length;
			if(len != arr2.length) return false;
			for(i=0; i<len; i++) {
				if(arr1[i] is Array && arr2[i] is Array) {
					if(!compareArr(arr1[i] , arr2[i])) {
						return false;
					}
				}else {
					if(arr1[i] != arr2[i]) {
						return false;
					}
				}
			}
			return true;
		}
		
		/**
		 * 返回数组 中最大的数
		 * @param int 
		 * 
		 */
		public static function getMax(arr:Array):int {
			var i:int =0; 
			var len:int = arr.length;
			var max:int = arr[0];
			for(i=1; i<len; i++) {
				if(arr[i] > max) {
					max = arr[i];
				} 
			}
			return max;
		}
		/**
		 * 返回数组 中最小的数
		 * @param int 
		 * 
		 */
		public static function getMin(arr:Array):int {
			var i:int =0; 
			var len:int = arr.length;
			var min:int = arr[0];
			for(i=1; i<len; i++) {
				if(arr[i] < min) {
					min = arr[i];
				} 
			}
			return min;
		}
		
		
		public static function add(arr1:Array,arr2:Array):Array{
			var arr:Array = new Array();
			if(arr1!=null){
				arr=arr.concat(arr1);
			}
			if(arr2!=null){
				arr=arr.concat(arr2);
			}
			return arr;
		}
		
		public static function clear(input:Array):void{
			input.length = 0;
		}
		
		public static function getValue(input:Array,row:int,column:int=0):Object{
			if(row<0||column<0){
				return null;
			}
			var obj:Object = input[row];
			if(obj == null){
				return null;
			}else if(obj is Array){
				return obj[column];
			}else{
				return (column==0)?obj:null;
			}
		}
		
		public static function insert(input:Array,index:uint,elem:Object):void{
			if(input.length<index){
				input.length = index;
			}
			input.splice(index,0,elem==null?undefined:elem);
		}
		public static function replace(input:Array,elem:Object,replaced:Object):Object{
			var len:uint = input.length;
			for(var i:uint = 0;i<len;i++){
				if(input[i]==replaced){
					input[i] = elem;
					return replaced;
				}
			}
			return null;	
		}
		
		public static function setValue(input:Array,value:Object,row:int,column:int=0,noMemoryIncrease:Boolean=false):void{
			if(row<0||column<0) return;
			if(noMemoryIncrease){
				if(input.length>row){
					if(input[row]!=null&&input[row].length>column){
						input[row][column] = value;
					}
				}
				return;
			}
			if(input[row] == null){
				input[row] = new Array();
			}
			if(input[row] is Array){
				input[row][column] = value;
			}else if(column==0){
				input[row] = column;
			}
		}
		
		public static  function trim(input:Array):Array{
			var i:uint,l:uint;
			var validateIndex:int = -1;
			for(i = 0,l=input.length;i<l;i++){
				if(input[i]!=null){
					validateIndex = i;
				}
			}
			input.length = validateIndex+1;
			return input;
		}
		public static function trim2D(input:Array):Array{
			trim(input);
			var i:uint,l:uint;
			for(i=0,l=input.length;i<l;i++){
				if(input[i]!=null&&(input[i] is Array)){
					trim(input[i] as Array);
				}
			}
			return input;
		}
		
		public static function get2DimensionsArrayWidth(input:Array):uint{
			var max:uint = 0;
			var i:uint,l:uint;
			for(i = 0,l = input.length;i<l;i++){
				var al:uint = 0;
				if(input[i]==null){
					continue;
				}
				if(input[i] is Array){
					al = (input[i] as Array).length;
				}else{
					al = 1; 
				}
				if(al>max){
					max = al;
				}
			}
			return max;
		}
		
		public static function getValueArray(input:Array,valueName:Object):Array{
			var arr:Array = new Array(input.length);
			for(var i:int=0,l:int=input.length;i<l;i++){
				var obj:Object = input[i];
				if(obj!=null){
					arr[i] = obj[valueName];
				}
			}
			return arr;
		}
		
		/**
		 * 给一维数数组每个元素 加上某个数
		 * @param numb
		 * @param arr
		 * 
		 */
		public static function addNumber(numb:int, arr:Array):void {
			var i:int;
			var len:int = arr.length;
			for(i=0; i<len; i++) {
				if(arr[i] is Number){
					arr[i] += numb;
				}
			}
		}
		
		/**
		 *打乱数组
		 * @param arr
		 * 
		 */
		public static function shuffle(arr:Array):void {
			var i:int;
			var len:int = arr.length;
			var seed:int;
			var temp:Object;
			for(i=0; i<len; i++) {
				temp = arr[i];
				seed = Math.random()*len;
				arr[i] = arr[seed];
				arr[seed] = temp;
			}
		}
		
		/**
		 *深拷贝一个对象 
		 * @param obj
		 * @return 
		 * 
		 */
		public static function copyObject(obj:Object):Object {
			var ba:ByteArray = new ByteArray();
			ba.writeObject(obj);
			ba.position = 0;
			var copyObj:Object = ba.readObject();
			return copyObj;
		}
		
		/**
		 *计算数组能分为多少页 
		 * @param len
		 * @return 
		 * 
		 */
		public static function getMaxPage(size:int, pageSize:int):int {
			var page:int = 1;
			page = (size + pageSize-1)/pageSize;
			if(page<1) {
				return 1;
			}
			return page;
		}
		
		/**
		 *循环arr中的每个元素作为func的参数执行func 结果替换原来的值
		 * @param arr
		 * @param func
		 * 
		 */
		public static function executeFunc(arr:Array, func:Function):void {
			var i:int;
			var len:int = arr.length;
			for(i=0; i<len; i++) {
				arr[i] = func(arr[i]);
			}
		}
		
		/**
		 *设置数组中的每个原属的属性  
		 * @param arr
		 * @param obj
		 * 
		 */
		public static function setArrayObjProp(list:Array, properties:String, value:Object):void {
			var arr:Array = list;
			var i:int;
			var len:int = arr.length;
			var obj:Object;
			for(i=0; i<len; i++) {
				obj = arr[i];
				obj[properties] = value;
			}
		}
		
		/**
		 * 网一个数组中添加 指定个数的整数
		 * @param arr 要操作的数组
		 * @param e
		 * @param len
		 * 
		 */
		public static function addElsByCount(arr:Array, e:int, len:int):void {
			var i:int;
			for(i=0; i<len; i++) {
				arr.push(e);
			}
		}
	}
}